from flask import Flask, request, jsonify
from settings import Sttings
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
#设置连接数据库的URL
app.config.from_object(Sttings)
#初始化dbs
db = SQLAlchemy(app)

# orm创表****************************************************************
class User(db.Model):
    class User(db.Model):
        __tablename__ = "user"
        id = db.Column(db.Integer, primary_key=True,autoincrement=True)
        name = db.Column(db.String(30), unique=True)
        age = db.Column(db.Integer,default=0)


# class lala(db.Model):
#     id = db.Column(db.Integer, primary_key=True,autoincrement=True)
#     name = db.Column(db.String(64), unique=True)
#     sex = db.Column(db.Boolean,default=True)
#     add_time = db.Column(db.DateTime)
#     content = db.Column(db.Text)
#     score = db.Column(db.Float)

# ******************************************************************************
#创建表
# db.create_all()
#删除表
# db.drop_all()

@app.route('/user',methods=['POST','GET'])
def add():
    if request.method == 'POST':
        data = request.form
        user = User(name=data['name'],age=data['age'])
        db.session.add(user)
        db.session.commit()
        return 'ok'
    else:
        list1 = []
        user = User.query.all()
        dict1 = {}
        for i in user:
            dict1['name'] = i.name
            dict1['age'] = i.age
            list1.append(dict1)
        print(list1)
        return "ok"



if __name__ == '__main__':
    app.run()
